<?php
class Admin_ArticleController extends Zend_Controller_Action {
	private $_model;
	private $_controllerName;
	private $_actionName;
	private $_params = array ();

	public function init() {
		$this->_helper->layout->setLayout ( 'admin' );
		$this->_sess = new Zend_Session_Namespace ();
		
		$this->_model = new Admin_Model_Article ();
		$this->_controllerName = $this->_request->getControllerName ();
		$this->_params = $this->_request->getParams ();
	}

	public function indexAction() {
	}

	public function addAction() {
		$this->view->headScript ()->appendFile ( $this->view->baseUrl () . '/public/media/backend/js/ckeditor/ckeditor.js' );
		
		$data = $this->_request->getPost ();
		
		$form = new Admin_Form_Article ();
		if ($this->_request->isPost ()) {
			$error = array ();
			$upload = new Zend_File_Transfer_Adapter_Http ();
			$upload->setDestination ( 'public/images/users/' );
			$upload->setValidators ( array (
					'size' => array (
							'min' => '20',
							'max' => '500000' 
					) 
			) )->addValidator ( 'Extension', false, array (
					'jpg',
					'jpeg',
					'png',
					'gif' 
			) );
			var_dump ( $form->getMessages () );
			if ($form->isValid ( $data ) && $upload->isValid ()) {
				$info = $upload->getFileInfo ();
				$fileName = $info ['image'] ['name'];
				$upload->receive ( $fileName );
				
				$data = $form->getValues ();
				
				// $data['user_id'] = $this->_sess->id;
				$data ['create_date'] = date ( 'Y-m-d H:i:s' );
				// var_dump($data); die;
				$articleId = $this->_model->add ( $data );
				if ($articleId) {
					$error = true;
				}
			}
			$this->view->isError = $error;
		}
		$this->view->form = $form;
	}
}
